package org.jeecg.modules.activiti.listener;

import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.jeecg.common.oa.utils.OaMessageUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.IActBusinessService;
import org.jeecg.modules.activiti.service.IActZprocessService;

import java.util.List;

public class InformedListener implements JavaDelegate {

    IActBusinessService actBusinessService;
    IActZprocessService actZprocessService;
    private TaskService taskService;

    @Override

    public void execute(DelegateExecution execution) throws Exception {

        if (actZprocessService == null) {
            actZprocessService = (IActZprocessService) SpringContextUtils.getBean("actZprocessServiceImpl");
        }
        if (actBusinessService == null) {
            actBusinessService = (IActBusinessService) SpringContextUtils.getBean("actBusinessServiceImpl");
        }

        if (taskService == null) {
            taskService = execution.getEngineServices().getTaskService();
        }
        List<LoginUser> users = actZprocessService.getNode(execution.getCurrentActivityId()).getUsers();
        if (users != null && users.size() > 0) {
            LoginUser formUser = new LoginUser();
            formUser.setId("-1");
            ActBusiness actBusiness = actBusinessService.getById(execution.getProcessBusinessKey());
            for (LoginUser user : users) {
//                System.out.println(execution.getTenantId());
//                taskService.addCandidateUser(execution.getId(), user.getUsername());
                OaMessageUtils.sendMessage(formUser, user, String.format("【知会】%s！", actBusiness.getTitle()), null, null);
            }
        }
//        // 避免重复添加
//        List<String> list = actBusinessService.selectIRunIdentity(execution.getCurrentActivityId(), ActivitiConstant.EXECUTOR_candidate);
//        System.out.println("serviceTask已经执行已经执行！");

//        String value1 = (String) text1.getValue(execution);
//
//        System.out.println(value1);
//
//        execution.setVariable("var1", new StringBuffer(value1).reverse().toString());

    }

}
